import pymysql
import cx_Oracle
from loguru import logger
class Mymysql:
    def __init__(self,host,user,password,db,port=3306,charset='utf8',autocommit=True):
        try:
            self.conn = pymysql.connect(host = host,user = user,password=password,db=db,port=port,charset=charset,autocommit=autocommit)
            self.cursor = self.conn.cursor(pymysql.cursors.DictCursor)
        except Exception as e:
            print('数据库连接失败！')
            raise Exception('数据库连接失败！%s'%e)

    def execute_other(self,sql):
        try:
            self.cursor.execute(sql)
        except Exception as e:
            print('sql语句有误！%s'%e)
    def execute_one(self,sql):
        try:
            self.cursor.execute(sql)
        except Exception as e:
            print('sql语句有误！%s'%e)
        else:
            return self.cursor.fetchone()
    def execute_many(self,sql):
        try:
            self.cursor.execute(sql)
        except Exception as e:
            print('sql语句有误！%s' % e)
        else:
            return self.cursor.fetchall()
    def table_exists(self,table_name):
        sql = "SELECT table_name FROM information_schema.TABLES WHERE table_name ='%s';"%table_name
        if self.execute_one(sql):
            return True
    def close(self):
        self.cursor.close()
        self.conn.close()

class MyOracle:
    def __init__(self,username,password,host,port,instance):
        try:
            self.conn = cx_Oracle.connect("%s/%s@%s:%s/%s" % (username,password,host,port,instance))
            self.cursor = self.conn.cursor()
        except Exception as e:
            logger.error("数据库连接出错了！", e)
    def execute_other(self,sql):
        try:
            self.cursor.execute(sql)
            self.conn.commit()
        except Exception as e:
            logger.error("sql语句%s出错了！"%sql, e)

    def execute_one(self,sql):
        try:
            self.cursor.execute(sql)
        except Exception as e:
            logger.error("sql语句%s出错了！"%sql, e)
        else:
            return self.cursor.fetchone()

    def execute_many(self, sql):
        try:
            self.cursor.execute(sql)
        except Exception as e:
            logger.error('sql语句有误！%s' % e)
        else:
            return self.cursor.fetchall()

    def table_exists(self, table_name):
        sql = "select count(*) from user_tables where table_name =upper('%s')" % table_name
        if self.execute_one(sql):
            return True

    def close(self):
        self.cursor.close()
        self.conn.close()
if __name__ == '__main__':
    import tools

    oracle_data = tools.parse_ini('oracle')
    oracle_data['port'] = int(oracle_data.get('port'))
    print(oracle_data)
    print(type(oracle_data))
    # '10.10.122.61','root','123456','cqs',port=3306
    a = MyOracle(**oracle_data)
    print(a.execute_many('select * from system_config'))

    # data = {
    #     "host" : "10.10.122.61",
    #     "user" : "root",
    #     "password" : "123456",
    #     "db" : "cqs",
    #     "port" : 3306
    # }
    # import tools
    # mysql_data = tools.parse_ini('mysql')
    # mysql_data['port']=int(mysql_data.get('port'))
    # print(mysql_data)
    # print(type(mysql_data))
    # #'10.10.122.61','root','123456','cqs',port=3306
    # a = Mymysql(**mysql_data)
    # print(a.execute_many('select * from system_config'))


